package de.windeln.marketing.campaign.model;

import java.io.IOException;
import java.lang.reflect.Type;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.common.base.Strings;

public class NullToEmptySerializer extends StdSerializer<String> {

	NullToEmptySerializer() {
		super(String.class);
	}

	@Override
	public void serialize(String value, JsonGenerator jgen,
			SerializerProvider provider) throws IOException,
			JsonGenerationException {
		if(Strings.isNullOrEmpty(value)){
			jgen.writeString("");
		}else{
			jgen.writeString(value);
		}
	}

	@Override
	public JsonNode getSchema(SerializerProvider provider, Type typeHint)
			throws JsonMappingException {
//		return createSchemaNode("null");
	   return createSchemaNode("string", true);
	}

	@Override
	public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor,
			JavaType typeHint) throws JsonMappingException {
//		visitor.expectNullFormat(typeHint);
		 if (visitor != null) visitor.expectStringFormat(typeHint);
	}

}
